package com.dl698.axdr;

import com.dl698.asn1.axdr.AxdrType;
import com.dl698.asn1.axdr.AxdrEnum;
import com.dl698.util.ReverseByteArrayInputStream;
import com.dl698.util.ReverseByteArrayOutputStream;

import java.io.IOException;

/**
 * ActionThenGetResult类
 * 实现动作后读取结果的编解码
 * 
 * @author wind
 * @version 1.1.1
 */
public class ActionThenGetResult implements AxdrType {

    private byte[] dataCode = null;
    private OMD omd = null;
    private AxdrEnum dar = null;
    private AxdrOptional<RETOMDDATA> retData = null;
    private ActionThenGetGetResult getResult = null;

    public ActionThenGetResult() {
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setAll(OMD omd, AxdrEnum dar, AxdrOptional<RETOMDDATA> retData, ActionThenGetGetResult getResult) {
        this.omd = omd;
        this.dar = dar;
        this.retData = retData;
        this.getResult = getResult;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) {
        int codeLength = 0;

        this.omd = new OMD();
        codeLength += this.omd.decode(input);

        this.dar = new AxdrEnum();
        this.dar.setConst();
        codeLength += this.dar.decode(input);

        this.retData = new AxdrOptional<RETOMDDATA>(RETOMDDATA.class);
        codeLength += this.retData.decode(input);

        this.getResult = new ActionThenGetGetResult();
        codeLength += this.getResult.decode(input);

        return codeLength;
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) {
        if (this.dataCode != null) {
            try {
                for (int i = this.dataCode.length - 1; i >= 0; i--) {
                    output.write(this.dataCode[i]);
                }
            } catch (IOException e) {
                throw new RuntimeException("Error writing dataCode to output stream", e);
            }
            return this.dataCode.length;
        }
        if (this.omd != null && this.dar != null && this.retData != null && this.getResult != null) {
            int codeLength = 0;
            codeLength += this.getResult.encode(output);
            codeLength += this.retData.encode(output);
            codeLength += this.dar.encode(output);
            codeLength += this.omd.encode(output);
            return codeLength;
        } else {
            throw new RuntimeException("omd || dar || retData || getResult is null");
        }
    }

    public void encodeAndSave(int encodingSizeGuess) {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        try {
            encode(revOStream);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException("Error encoding ActionThenGetResult", e);
        }
        this.dataCode = revOStream.getArray();
    }

    @Override
    public String toString() {
        if (this.omd != null && this.dar != null && this.retData != null && this.getResult != null) {
            return "sequence: {" + "omd: " + this.omd + ", dar: " + this.dar + ", ret_data: " + this.retData + ", get_result: " + this.getResult + "}";
        } else {
            return "omd || dar || ret_data || get_result is null";
        }
    }

    public AxdrEnum getDar() {
        return dar;
    }

    public AxdrOptional<RETOMDDATA> getRetData() {
        return retData;
    }

    public ActionThenGetGetResult getGetResult() {
        return getResult;
    }
}